package com.qianfeng.webflux.service.impl;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.qianfeng.webflux.pojo.R;
import com.qianfeng.webflux.pojo.SysUserInfo;
import com.qianfeng.webflux.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.query.Query;
import org.springframework.data.relational.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * Created by Jackiechan on 2022/7/8 14:43
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Service
@Transactional
public class SysUserServiceImpl implements SysUserService {

    private R2dbcEntityTemplate r2dbcEntityTemplate;

    @Autowired
    public void setR2dbcEntityTemplate(R2dbcEntityTemplate r2dbcEntityTemplate) {
        this.r2dbcEntityTemplate = r2dbcEntityTemplate;
    }

    @Override
    public Mono<SysUserInfo> findById(Long id) {
            //select * from xxx where id =xxx
        //我们不写表名仍旧可以查询到数据,原因是因为把我们的类名从驼峰转成_格式就是表名
        return r2dbcEntityTemplate.selectOne(Query.query(Criteria.where("u_id").is(id)), SysUserInfo.class);
    }

    @Override
    public Flux<SysUserInfo> findAll() {
        return r2dbcEntityTemplate.select(SysUserInfo.class).from("sys_user_info").all();
    }

    @Override
    public Mono<SysUserInfo> addUser(SysUserInfo sysUserInfo) {
      return  r2dbcEntityTemplate.insert(SysUserInfo.class).into("sys_user_info").using(sysUserInfo);
    }

    @Override
    public Mono<Integer> deleteById(Long id) {
        Mono<Integer> mono = r2dbcEntityTemplate.delete(SysUserService.class)
                .from("sys_user_info")
                .matching(Query.query(Criteria.where("u_id").is(id))).all();
        return mono;
    }
    //update xxxbiao set xxx=xxx,xxx=xxxx where xxxx=xxxx
    @Override
    public Mono<Integer> updateUser(SysUserInfo sysUserInfo) {
        Mono<Integer> integerMono = r2dbcEntityTemplate.update(SysUserService.class).inTable("sys_user_info")
                //设置更新的条件
                .matching(Query.query(Criteria.where("u_id").is(sysUserInfo.getUId())))
                //设置要更新的列
                .apply(Update.update("phone", sysUserInfo.getPhone()).set("email", sysUserInfo.getEmail()));
        return integerMono;
    }

    @Override
    public Mono<R> addUser2(Mono<SysUserInfo> userInfoMono) {
//        return userInfoMono
//                .flatMap(sysUserInfo -> r2dbcEntityTemplate.insert(SysUserInfo.class).into("sys_user_info").using(sysUserInfo))
//                .flatMap(sysUserInfo -> Mono.just(new R(1,"success",null)));
        userInfoMono.subscribe(sysUserInfo ->
                r2dbcEntityTemplate.insert(SysUserInfo.class).into("sys_user_info").using(sysUserInfo).subscribe());
        return Mono.just(new R(1, "success", null));
    }
}
